package com.itzyf.pokemondata.activity

import android.graphics.Color
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.ResistanceDo
import com.itzyf.pokemondata.utils.ResistanceUtils
import com.itzyf.pokemondata.utils.subZeroAndDot
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.tydic.baselibrary.activity.BaseActivity
import com.tydic.baselibrary.utils.DividerGridItemDecoration
import com.tydic.baselibrary.utils.MyGridLayoutManager
import kotlinx.android.synthetic.main.activity_resistance.*
import kotlin.math.max


/**
 * @author 依风听雨
 * @version 创建时间：2019/01/11 09:37
 */
class ResistanceActivity : BaseActivity() {

    private val property = arrayOf(
        "一般", "格斗", "飞行", "毒", "地面", "岩石",
        "虫", "幽灵", "钢", "火", "水", "草",
        "电", "超能力", "冰", "龙", "恶", "妖精"
    )
    private val data: MutableList<MutableList<String>> = ArrayList()
    private var primaryAttr = -1
    private var subAttr = -1
    private var subAttr3 = -1
    private var subAttr4 = -1
    private var subAttr5 = -1

    private lateinit var mFaceAdapter: QuickAdapter
    private lateinit var mResistanceAdapter: QuickAdapter


    var mFaceDatas: MutableList<ResistanceDo?> = ArrayList()
    var mResistanceDatas: MutableList<ResistanceDo?> = ArrayList()

    override fun getContentViewId() = com.itzyf.pokemondata.R.layout.activity_resistance

    override fun getToolbarTitle() = "属性相克表"

    override fun handlerActivity() {
        println(Gson().toJson(ResistanceUtils))
        for (row in ResistanceUtils) {
            val rowNew: MutableList<String> = ArrayList()
            for (item in row) {
                rowNew.add(item)
            }
            data.add(rowNew)
        }

        tvAttr1.hint = "请选择主属性"
        tvAttr1.setOnClickListener {
            val builder = QMUIDialog.CheckableDialogBuilder(this)
                .addItems(property) { dialog, which ->
                    if (subAttr == which) {
                        showToast("主属性不能和副属性一样")
                        return@addItems
                    }
                    tvAttr1.text = property[which]
                    primaryAttr = which
                    calculate()
                    dialog.dismiss()
                }
            builder.show()
        }

        tvAttr2.hint = "请选择第二属性"
        tvAttr2.setOnClickListener {
            if (primaryAttr == -1) {
                showToast("请先选择主属性")
                return@setOnClickListener
            }
            val builder = QMUIDialog.CheckableDialogBuilder(this)
                .addItems(property) { dialog, which ->
                    if (primaryAttr == which) {
                        showToast("副属性不能和主属性一样")
                        return@addItems
                    }
                    tvAttr2.text = property[which]
                    subAttr = which
                    calculate()
                    dialog.dismiss()
                }
            builder.show()
        }

        tvAttr3.hint = "请选择第三属性"
        tvAttr3.setOnClickListener {
            val builder = QMUIDialog.CheckableDialogBuilder(this)
                .addItems(property) { dialog, which ->
                    tvAttr3.text = property[which]
                    subAttr3 = which
                    calculate()
                    dialog.dismiss()
                }
            builder.show()
        }

        tvAttr4.hint = "请选择第四属性"
        tvAttr4.setOnClickListener {
            val builder = QMUIDialog.CheckableDialogBuilder(this)
                .addItems(property) { dialog, which ->
                    tvAttr4.text = property[which]
                    subAttr4 = which
                    calculate()
                    dialog.dismiss()
                }
            builder.show()
        }
        tvAttr5.hint = "请选择第五属性"
        tvAttr5.setOnClickListener {
            val builder = QMUIDialog.CheckableDialogBuilder(this)
                .addItems(property) { dialog, which ->
                    tvAttr5.text = property[which]
                    subAttr5 = which
                    calculate()
                    dialog.dismiss()
                }
            builder.show()
        }
        for (i in 0..17) {
            mFaceDatas.add(ResistanceDo(property[i], 0.0))
            mResistanceDatas.add(ResistanceDo(property[i], 0.0))
        }

        mFaceAdapter = QuickAdapter(com.itzyf.pokemondata.R.layout.item_text2, mFaceDatas)
        mResistanceAdapter =
            QuickAdapter(com.itzyf.pokemondata.R.layout.item_text2, mResistanceDatas)

        rvFace.adapter = mFaceAdapter
        rvResistance.adapter = mResistanceAdapter

        rvFace.layoutManager = MyGridLayoutManager(this, 3)
        rvResistance.layoutManager = MyGridLayoutManager(this, 3)

        rvFace.addItemDecoration(DividerGridItemDecoration(this))
        rvResistance.addItemDecoration(DividerGridItemDecoration(this))

    }


    private fun calculate() {
        calculateFace()
        calculateResistance()
    }

    /**
     * 计算打击面
     */
    private fun calculateFace() {
        mFaceDatas.clear()

        val p = arrayOf(primaryAttr, subAttr, subAttr3, subAttr4, subAttr5)

        for (i in 0..17) {
            val d = p.filter { it != -1 }
                .map { data[it][i].toDouble() }
                .max()
            mFaceDatas.add(ResistanceDo(property[i], d!!))

        }
        mFaceAdapter.notifyDataSetChanged()
    }

    /**
     * 计算抗性
     */
    private fun calculateResistance() {
        mResistanceDatas.clear()
        val p = arrayOf(primaryAttr, subAttr, subAttr3, subAttr4, subAttr5)

        for (i in 0..17) {
            val d = p.filter { it != -1 }
                .map { data[i][it].toDouble() }
                .reduce { acc, d -> acc * d }
            mResistanceDatas.add(ResistanceDo(property[i], d))
        }
        mResistanceAdapter.notifyDataSetChanged()
    }


    internal inner class QuickAdapter(layoutResId: Int, data: List<ResistanceDo?>) :
        BaseQuickAdapter<ResistanceDo, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: ResistanceDo?) {
            item?.let {
                helper.setText(R.id.tvAttr, "${it.property} :")
                helper.setText(R.id.tvValue, it.value.subZeroAndDot())

                when {
                    it.value == 0.0 -> helper.setTextColor(
                        R.id.tvValue,
                        resources.getColor(R.color.re0)
                    )
                    it.value == 1.0 -> helper.setTextColor(
                        R.id.tvValue,
                        resources.getColor(R.color.re1)
                    )
                    it.value > 1 -> helper.setTextColor(
                        R.id.tvValue,
                        resources.getColor(R.color.re2)
                    )
                    else -> helper.setTextColor(R.id.tvValue, resources.getColor(R.color.re05))
                }
            }

        }
    }


}
